package de.revdev.llt.bean.facade;

import de.revdev.llt.bean.entities.FileCardBox;
import de.revdev.llt.bean.entities.LearnStatusFCB;
import de.revdev.llt.bean.entities.User;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Root;

/**
 *
 * @author f.lindermaier
 */
@Stateless
public class LearnStatusFCBFacade extends AbstractFacade<LearnStatusFCB> {

    @PersistenceContext(unitName = "LetsLearnTogetherPU")
    private EntityManager em;

    @Override
    public EntityManager getEntityManager() {
        return em;
    }

    public LearnStatusFCBFacade() {
        super(LearnStatusFCB.class);
    }

    public LearnStatusFCB getLearnStatusForFileCardBox(User user, FileCardBox fileCardBox) {
        try {
            CriteriaBuilder cb = em.getCriteriaBuilder();
            CriteriaQuery<LearnStatusFCB> cq = cb.createQuery(LearnStatusFCB.class);
            Root<LearnStatusFCB> r = cq.from(LearnStatusFCB.class);
            cq.select(r);
            ParameterExpression<User> p = cb.parameter(User.class);
            ParameterExpression<FileCardBox> q = cb.parameter(FileCardBox.class);
            cq.where(cb.equal(r.get("user"), p), cb.equal(r.get("fileCardBox"), q));
            return (LearnStatusFCB) em.createQuery(cq).setParameter(p, user).setParameter(q, fileCardBox).getSingleResult();
        } catch (Exception e) {
            return null;
        }
    }
}
